package com.waimai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.waimai.common.R;
import com.waimai.domain.Category;
import com.waimai.domain.Dish;
import com.waimai.service.CategoryService;
import com.waimai.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    DishService dishService;


    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){
        //分页器
        Page page1 = new Page<>(page,pageSize);
        //
        LambdaQueryWrapper<Category> queryWrapper= new LambdaQueryWrapper();
        //排序条件
        queryWrapper.orderByDesc(Category::getSort);
        //执行查询
       categoryService.page(page1,queryWrapper);
        return R.success(page1);
    }
    //删除套餐、餐品
     @DeleteMapping
     public R<String> delete(Long  ids){

         categoryService.remove(ids);
        return R.success("1");
     }

    @PutMapping
    public R<String> updata(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("1");
    }

    @GetMapping("/list")
    public R<List<Dish>> list(int type){
        Category byId = categoryService.getById(type);
        List<Dish> list = dishService.list();

        return R.success(list);
    }


}
